package com.huawei.petapp2.fragment;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.huawei.petapp2.R;
import com.huawei.petapp2.adapter.CardAdapter;
import com.huawei.petapp2.entity.ProductVo;
import com.huawei.petapp2.entity.TestBean1;
import com.huawei.petapp2.utils.OverLayCardLayoutManager;
import com.huawei.petapp2.view.CardConfig;
import com.huawei.petapp2.view.TanTanCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author: njb
 * @date: 2020/3/24 0024 10:47
 * @desc: 产品案例
 */
public class FMProductCase extends BaseFragment {
    @BindView(R.id.rv_product)
    RecyclerView rvProduct;
    private CardAdapter cardAdapter;
    Context context;
    ProductVo storeitem;

    protected void loadData() {
        List<TestBean1> datas = new ArrayList<>();
        //for(int i=0;i<10;i++){
            TestBean1 swipeCardBean = new TestBean1();
            swipeCardBean.setUrl( storeitem.getProductImgs().get(0).getUrl());
            swipeCardBean.setName(storeitem.getProduct().getProductName());
            datas.add(swipeCardBean);
      //  }
        cardAdapter = new CardAdapter(datas);
        rvProduct.setLayoutManager(new OverLayCardLayoutManager());
        rvProduct.setAdapter(cardAdapter);
        CardConfig.initConfig(context);
        final TanTanCallback callback = new TanTanCallback(rvProduct, cardAdapter, datas);
        final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(callback);
        itemTouchHelper.attachToRecyclerView(rvProduct);
        Log.d("--count--",  getCount(rvProduct) + "");

    }

    @Override
    public void initView(View mView) {
        rvProduct=mView.findViewById(R.id.rv_product);
        this.context=mView.getContext();
        initAdapter();
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        if (arguments != null) {
            this.storeitem= (ProductVo) arguments.getSerializable("storeitem");
            System.out.println("传输过来的storeitem"+storeitem);
        }
    }
    public int getLayoutId() {
        return R.layout.fm_product_case;
    }

    private void initAdapter() {
        List<TestBean1> datas = new ArrayList<>();
        for(int i=0;i<storeitem.getProductImgs().size();i++){
            TestBean1 swipeCardBean = new TestBean1();
            swipeCardBean.setUrl(storeitem.getProductImgs().get(i).getUrl());
            swipeCardBean.setName(storeitem.getProduct().getProductName());
            datas.add(swipeCardBean);
        }
        cardAdapter = new CardAdapter(datas);
        rvProduct.setLayoutManager(new OverLayCardLayoutManager());
        rvProduct.setAdapter(cardAdapter);
        CardConfig.initConfig(context);
        final TanTanCallback callback = new TanTanCallback(rvProduct, cardAdapter, datas);
        final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(callback);
        itemTouchHelper.attachToRecyclerView(rvProduct);
        Log.d("--count--",  getCount(rvProduct) + "");

    }

    private int getCount(ViewGroup viewGroup){
        int count = viewGroup.getChildCount();
        for(int i=0;i<count;i++){
            View child=viewGroup.getChildAt(i);
            if(child instanceof ViewGroup){
                //如果子View是ViewGroup，则用递归获取子View数量
                int childCount = getCount((ViewGroup)child);
                count+=childCount;
            }else {
                count++;
            }
        }
        return count;
    }


    protected void addListener() {

    }
}
